/**
 * Copyright © 2016-2025 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.terracloud.server.dao.sql.log;

import com.terracloud.server.common.data.log.LogLevel;
import com.terracloud.server.common.data.log.LogType;
import com.terracloud.server.common.data.log.LogStatus;
import com.terracloud.server.dao.model.sql.TerminalLogEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.UUID;

public interface TerminalLogRepository extends JpaRepository<TerminalLogEntity, UUID> {

    @Query("SELECT t FROM TerminalLogEntity t WHERE t.tenantId = :tenantId AND t.deviceId = :deviceId AND (:startTime IS NULL OR t.createdTime >= :startTime) AND (:endTime IS NULL OR t.createdTime <= :endTime) ORDER BY t.createdTime DESC")
    Page<TerminalLogEntity> findByDeviceId(@Param("tenantId") UUID tenantId, @Param("deviceId") UUID deviceId, @Param("startTime") Long startTime, @Param("endTime") Long endTime, Pageable pageable);

    @Query("SELECT t FROM TerminalLogEntity t WHERE t.tenantId = :tenantId AND (:startTime IS NULL OR t.createdTime >= :startTime) AND (:endTime IS NULL OR t.createdTime <= :endTime) ORDER BY t.createdTime DESC")
    Page<TerminalLogEntity> findByTenantId(@Param("tenantId") UUID tenantId, @Param("startTime") Long startTime, @Param("endTime") Long endTime, Pageable pageable);

    @Query("SELECT t FROM TerminalLogEntity t WHERE t.tenantId = :tenantId AND t.logType = :logType AND (:startTime IS NULL OR t.createdTime >= :startTime) AND (:endTime IS NULL OR t.createdTime <= :endTime) ORDER BY t.createdTime DESC")
    Page<TerminalLogEntity> findByTenantIdAndType(@Param("tenantId") UUID tenantId, @Param("logType") LogType logType, @Param("startTime") Long startTime, @Param("endTime") Long endTime, Pageable pageable);

    @Query("SELECT t FROM TerminalLogEntity t WHERE t.tenantId = :tenantId AND t.logLevel = :logLevel AND (:startTime IS NULL OR t.createdTime >= :startTime) AND (:endTime IS NULL OR t.createdTime <= :endTime) ORDER BY t.createdTime DESC")
    Page<TerminalLogEntity> findByTenantIdAndLevel(@Param("tenantId") UUID tenantId, @Param("logLevel") LogLevel logLevel, @Param("startTime") Long startTime, @Param("endTime") Long endTime, Pageable pageable);

    @Query("SELECT t FROM TerminalLogEntity t WHERE t.tenantId = :tenantId AND t.deviceId = :deviceId AND t.logType = :logType AND (:startTime IS NULL OR t.createdTime >= :startTime) AND (:endTime IS NULL OR t.createdTime <= :endTime) ORDER BY t.createdTime DESC")
    Page<TerminalLogEntity> findByTenantIdAndDeviceIdAndType(@Param("tenantId") UUID tenantId, @Param("deviceId") UUID deviceId, @Param("logType") LogType logType, @Param("startTime") Long startTime, @Param("endTime") Long endTime, Pageable pageable);

    @Query("SELECT t FROM TerminalLogEntity t WHERE t.tenantId = :tenantId AND t.deviceId = :deviceId AND t.logLevel = :logLevel AND (:startTime IS NULL OR t.createdTime >= :startTime) AND (:endTime IS NULL OR t.createdTime <= :endTime) ORDER BY t.createdTime DESC")
    Page<TerminalLogEntity> findByTenantIdAndDeviceIdAndLevel(@Param("tenantId") UUID tenantId, @Param("deviceId") UUID deviceId, @Param("logLevel") LogLevel logLevel, @Param("startTime") Long startTime, @Param("endTime") Long endTime, Pageable pageable);

    @Query("SELECT COUNT(t) FROM TerminalLogEntity t WHERE t.tenantId = :tenantId AND t.processStatus = :processStatus")
    long countByProcessStatus(@Param("tenantId") UUID tenantId, @Param("processStatus") LogStatus processStatus);

    @Query("SELECT COUNT(t) FROM TerminalLogEntity t WHERE t.tenantId = :tenantId AND t.isCritical = true")
    long countByCritical(@Param("tenantId") UUID tenantId);

    @Query("SELECT COUNT(t) FROM TerminalLogEntity t WHERE t.tenantId = :tenantId AND t.duration > :duration")
    long countByDurationGreaterThan(@Param("tenantId") UUID tenantId, @Param("duration") long duration);
}